/**
 *  Copyright 2010 ShenYang voole Co. Ltd.
 *  All right reserved. 
 */
package com.wm.dao.hibernate;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wm.bean.Area;
import com.wm.bean.City;
import com.wm.bean.MenuType;
import com.wm.comm.Constant;
import com.wm.dao.AreaDAO;
import com.wm.dao.CityDAO;
import com.wm.dao.MenuTypeDAO;

/**
 * 功能描述:菜单分类数据操作
 *
 * 创建日期 Dec 29, 2012
 *
 * @author zhangbaochao
 *
 * @version 1.0
 *
 */
public class MenuTypeHibernateDAO extends HibernateDaoSupport implements MenuTypeDAO{
	
	private static final String LOAD_BY_ID = "from MenuType where id = ?";
	private static final String  REMOVE_BY_ID = "delete MenuType where id = ?";
	/**
	 * 保存MenuType对象
	 * 
	 * @param menuType
	 * 			MenuType
	 * @return MenuType
	 * 			MenuType
	 */
	public MenuType saveMenuType(MenuType menuType){
		this.getHibernateTemplate().save(menuType);
		return menuType;
	}
	
	/**
	 * 更新MenuType对象
	 * 
	 * @param menuType
	 * 			MenuType
	 * @return MenuType
	 * 			MenuType
	 */
	public MenuType updateMenuType(MenuType menuType){
		this.getHibernateTemplate().update(menuType);
		return menuType;
	}
	
	/**
	 * 根据ID删除MenuType对象
	 * 
	 * @param id
	 * 			long
	 */
	public void removeMenuTypeById(final long id){
		getHibernateTemplate().execute(new HibernateCallback() {
	        public Object doInHibernate(Session s) throws HibernateException, SQLException {
	          Query query = s.createQuery(REMOVE_BY_ID);
	          query.setLong(0, id);
	          query.executeUpdate();
	          return null;
	        }
	      });
	}
	
	/**
	 * 根据ID获取MenuType对象
	 * 
	 * @param id
	 * 			long 
	 * @return MenuType
	 * 			MenuType
	 */
	public MenuType findMenuTypeById(long id){
		List l = this.getHibernateTemplate().find(LOAD_BY_ID,id);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			return (MenuType) l.get(0);
		}
	}
	
	/**
	 * 根据参数获取MenuType对象列表
	 * 
	 * @param isShow
	 * 			int
	 * @param orderby
	 * @param ascOrDesc
	 * @return List
	 * 			List
	 */
	public List findMenuTypeByIsShow(int isShow, String orderby, int ascOrDesc){
		StringBuffer sb = new StringBuffer();
		sb.append("from MenuType where 1=1");
		if (isShow != -1) {
			sb.append(" and isShow=");
			sb.append(isShow);
		}
		if (StringUtils.isNotBlank(orderby)) {
			if (ascOrDesc == Constant.ORDER_ASC) {
				sb.append(" order by " + orderby+" asc");
			}
			if (ascOrDesc == Constant.ORDER_DESC) {
				sb.append(" order by " + orderby+" desc");
			}
		}
		final String sql = sb.toString();
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException, SQLException {
				Query query = s.createQuery(sql);
				List list = query.list();
				return list;
			}
		});
	}
}
